package com.rh.controller;

import com.rh.Enums;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.Department;
import com.rh.excrptions.DoException;
import com.rh.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin
@ResponseBody
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    /**
     * 分页
     */
    @RequestMapping(value = "/getDepartmentPageJson",method = RequestMethod.GET)
    public Result getDepartmentPageJson(Integer pageNo,Integer pageSize) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 2;
            }
            Page departmentPage = departmentService.getDepartmentPage(pageNo, pageSize);
            Result result = new Result();
            result.setData(departmentPage);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
    /**
     * 添加部门
     */
    @RequestMapping(value = "/add_department",method = RequestMethod.POST)
    public Result addDepartment(Department department){
        try{
            departmentService.addMethod(department);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.ADD_ERROR);
        }
    }
    /**
     * 更新部门
     */
    @RequestMapping(value = "/update_department",method = RequestMethod.POST)
    public Result updateDepartment(Department department){
        try{
            departmentService.updateMethod(department);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.UPDATE_ERROR);
        }
    }
    /**
     *  删除部门
     */
    @RequestMapping(value = "/delete_department",method = RequestMethod.GET)
    public Result deleteDepartment(Integer deptId){
        try{
            departmentService.deleteMethod(deptId);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.DELETE_ERROR);
        }
    }
    /**
     * 根据id查询部门信息
     */
    @RequestMapping(value = "/getDepartmentById",method = RequestMethod.GET)
    public Result getDepartmentById(Integer deptId){
       try {
           Department methodAllById = departmentService.getMethodAllById(deptId);
           Result result = new Result();
           result.setData(methodAllById);
           return result;
       }catch (Exception e){
           throw new DoException(Enums.GETLIST_ERROR);
       }
       }
}
